package com.ocom.accesservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.DeptInfoService;
import com.ocom.accesservice.service.MjRegionInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityDeptInfo;
import com.ocom.common.entity.access.EntityMjRegionInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.human.GetMjRegionByIdsRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class RegionPageController {

    @Autowired
    private MjRegionInfoService mjRegionInfoService;

    @Autowired
    private DeptInfoService deptInfoService;

    /*
     * @Description:获取片区信息（getRegionInfo）
     * @Author: lu 
     * @Date: 2022/4/20 10:59
     * @param object:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_getRegionInfo)
    public Result getRegionInfo(@RequestBody JSONObject object){
        if (object.getLong("comId")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        JSONObject reObject=new JSONObject();
        List<EntityMjRegionInfo> list=new ArrayList<>();
        if (object.getInteger("size")==null || object.getInteger("page")==null || object.getInteger("page")==-1 || object.getInteger("size")==-1){
            list=mjRegionInfoService.selectMjRegionInfoList(object.getLong("comId"),object);
        }else {
            PageInfo<EntityMjRegionInfo> pageInfoList=mjRegionInfoService.selectPageInfoList(object.getLong("comId"),object);
            list=pageInfoList.getList();
            reObject.put("total",pageInfoList.getTotal());
        }
        List<JSONObject> objectList=new ArrayList<>();
        for (EntityMjRegionInfo entityMjRegionInfo : list) {
            JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(entityMjRegionInfo));
            EntityDeptInfo entityDeptInfo=deptInfoService.selectDeptName(object.getLong("comId"),entityMjRegionInfo.getDeptId());
            jsonObject.put("createTime", DateUtil.getStrDate(entityMjRegionInfo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            jsonObject.put("updateTime",DateUtil.getStrDate(entityMjRegionInfo.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
            if (entityDeptInfo!=null){
                jsonObject.put("deptName",entityDeptInfo.getDeptName());
            }
            objectList.add(jsonObject);
        }

        reObject.put("list",objectList);
        return ResultUtil.success(reObject);
    }

    /*
     * @Description:2.2.5添加片区（newRegionInfo）
     * @Author: lu 
     * @Date: 2022/4/20 13:52
     * @param object:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_newRegionInfo)
    public Result newRegionInfo(@RequestBody JSONObject object){
        if (object.getLong("comId")==null || object.getLong("deptId")==null || object.getString("regionName")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        EntityMjRegionInfo entityMjRegionInfo=mjRegionInfoService.selectRegionName(object.getLong("comId"),object.getString("regionName"));
        if (entityMjRegionInfo!=null){
            return ResultUtil.error(ResultEnum.Err_3027);
        }
        EntityMjRegionInfo saveEntityMjRegionInfo=JSON.parseObject(object.toJSONString(),EntityMjRegionInfo.class);
        Date date=new Date();
        saveEntityMjRegionInfo.setCreateTime(date);
        saveEntityMjRegionInfo.setUpdateTime(date);
        int i=mjRegionInfoService.insertEntityMjRegionInfo(object.getLong("comId"),saveEntityMjRegionInfo);
        if (i>0){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_2016);
        }
    }


    /*
     * @Description:2.2.6修改片区（updateRegionInfo）
     * @Author: lu 
     * @Date: 2022/4/20 14:47
     * @param object:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_updateRegionInfo)
    public Result updateRegionInfo(@RequestBody JSONObject object){
        if (object.getLong("comId")==null || object.getLong("deptId")==null || object.getString("regionName")==null || object.getLong("regionId")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        EntityMjRegionInfo entityMjRegionInfo=mjRegionInfoService.selectRegionName(object.getLong("comId"),object.getString("regionName"));
        if (entityMjRegionInfo!=null){
            if (entityMjRegionInfo.getRegionId()!=object.getLong("regionId")){
                return ResultUtil.error(ResultEnum.Err_3027);
            }
        }
        EntityMjRegionInfo saveEntityMjRegionInfo=JSON.parseObject(object.toJSONString(),EntityMjRegionInfo.class);
        saveEntityMjRegionInfo.setUpdateTime(new Date());
        int i=mjRegionInfoService.updateEntityMjRegionInfo(object.getLong("comId"),saveEntityMjRegionInfo);
        if (i>0){
            Map<String,String> map=new HashMap<>();
            map.put("newName",object.getString("regionName"));
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    /*
     * @Description:移除片区（removeRegionInfo）
     * @Author: lu 
     * @Date: 2022/4/20 16:55
     * @param object:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_removeRegionInfo)
    public Result removeRegionInfo(@RequestBody JSONObject object){
        if (object.getLong("comId")==null || object.getLong("regionId")==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        int i=mjRegionInfoService.removeRegionInfo(object.getLong("comId"),object.getLong("regionId"));
        if (i>0){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.Err_3026);
        }
    }

    @PostMapping(AccessServiceUrl.URL_getMjRegionInfo)
    public Result getMjRegionInfo(@RequestBody EntityMjRegionInfo entityMjRegionInfo){
        return ResultUtil.success(this.mjRegionInfoService.selectMjRegionInfo(entityMjRegionInfo.getComId(), entityMjRegionInfo));
    }


    @PostMapping(AccessServiceUrl.URL_getMjRegionByIds)
    public Result getMjRegionByIds(@RequestBody GetMjRegionByIdsRequest request){
        return ResultUtil.success(this.mjRegionInfoService.selectMjRegionByIds(request.getComId(), request.getIds()));
    }
}
